﻿using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;

namespace Odyssey.Http.Implementations
{
    internal partial class HttpRequester
    {
        public async Task<HttpResponseMessage> DeleteAsync(string uri, string data, string clientName = "", IDictionary<string, string> requestHeaders = null)
        {
            return await DeleteAsync(new Uri(uri), data, clientName, requestHeaders);
        }

        public async Task<HttpResponseMessage> DeleteAsync(Uri uri, string data, string clientName = "", IDictionary<string, string> requestHeaders = null)
        {
            return await SendAsync(uri, data, HttpMethod.Delete, clientName, requestHeaders: requestHeaders);
        }

        public async Task<T> DeleteAsync<T>(string uri, object data, string clientName = "", IDictionary<string, string> requestHeaders = null, IHttpSerializer httpSerializer = null)
        {
            return await DeleteAsync<T>(new Uri(uri), data, clientName, requestHeaders, httpSerializer);
        }

        public async Task<T> DeleteAsync<T>(Uri uri, object data, string clientName = "", IDictionary<string, string> requestHeaders = null, IHttpSerializer httpSerializer = null)
        {
            httpSerializer = httpSerializer ?? _httpSerializer;
            var httpResponseMessage = await DeleteAsync(uri, httpSerializer.Serialize(data), clientName, requestHeaders);
            var responseJsonString = await httpResponseMessage.Content.ReadAsStringAsync();
            if (_logger.IsEnabled(LogLevel.Information))
            {
                _logger.LogInformation($"get uri=[{uri.ToString()}] response data=[{responseJsonString}]");
            }
            return httpSerializer.Deserialize<T>(responseJsonString);
        }

        public async Task<TResponse> DeleteAsync<TRequest, TResponse>(string uri, TRequest data, string clientName = "", IDictionary<string, string> requestHeaders = null, IHttpSerializer httpSerializer = null)
        {
            return await DeleteAsync<TRequest, TResponse>(new Uri(uri), data, clientName, requestHeaders, httpSerializer);
        }

        public async Task<TResponse> DeleteAsync<TRequest, TResponse>(Uri uri, TRequest data, string clientName = "", IDictionary<string, string> requestHeaders = null, IHttpSerializer httpSerializer = null)
        {
            httpSerializer = httpSerializer ?? _httpSerializer;
            var httpResponseMessage = await DeleteAsync(uri, httpSerializer.Serialize(data), clientName, requestHeaders);
            var responseJsonString = await httpResponseMessage.Content.ReadAsStringAsync();
            if (_logger.IsEnabled(LogLevel.Information))
            {
                _logger.LogInformation($"get uri=[{uri.ToString()}] response data=[{responseJsonString}]");
            }
            return httpSerializer.Deserialize<TResponse>(responseJsonString);
        }

        public virtual async Task<string> DeleteStringAsync(string uri, string data, string clientName = "", IDictionary<string, string> requestHeaders = null)
        {
            return await DeleteStringAsync(new Uri(uri), data, clientName, requestHeaders);
        }

        public virtual async Task<string> DeleteStringAsync(Uri uri, string data, string clientName = "", IDictionary<string, string> requestHeaders = null)
        {
            var responseMessage = await DeleteAsync(uri, data, clientName, requestHeaders);
            var jsonContent = await responseMessage.Content.ReadAsStringAsync();
            if (_logger.IsEnabled(LogLevel.Information))
            {
                _logger.LogInformation($"post uri=[{uri.ToString()}] responsedata[{jsonContent}]");
            }
            return jsonContent;
        }
    }
}
